﻿#region Using

using System.Collections.Generic;
using System.Linq;

using Common.Base;
using Common.Enums;

using GameCommon.Args;
using GameCommon.Base;
using GameCommon.Components;
using GameCommon.Manager;

using Menu.Components;

using Microsoft.Xna.Framework.Input;

using Xna = Microsoft.Xna.Framework;

#endregion

namespace Menu.Screens
{
    public class Options : ScreenBase
    {
        private readonly Dictionary<string, TextInput> inputs;

        public Options(IScreenChanger changer, object argument)
            : base(changer, argument)
        {
            this.inputs = new Dictionary<string, TextInput>();
            Managers.KeyboardManager.KeyPressed += this.OnKeyPressed;
        }

        public override ScreenEnum Screen
        {
            get
            {
                return ScreenEnum.Options;
            }
        }

        public override void LoadContent()
        {
            this.Components = MenuLoader.Get(ScreenEnum.Options);

            this.inputs.Add("TI_Name", (TextInput)this.Components["TI_Name"]);
            this.inputs.Add("TI_Tag", (TextInput)this.Components["TI_Tag"]);

            this.AddCommand("Name", this.OnName);
            this.AddCommand("Tag", this.OnTag);
            this.AddCommand("Back", this.OnBack);
        }

        public override void Update(Xna.GameTime gameTime)
        {
            base.Update(gameTime);

            foreach (var textInput in this.inputs)
            {
                textInput.Value.Update(gameTime);
            }
        }

        private void OnBack(GameComponent sender)
        {
            this.RemoveScreen(this.Screen);
            this.AddScreen(ScreenEnum.MainMenu);

            Managers.KeyboardManager.KeyPressed -= this.OnKeyPressed;
        }

        private void OnName(GameComponent sender)
        {
            this.ControlInputController(this.inputs["TI_Name"]);
        }

        private void OnTag(GameComponent sender)
        {
            this.ControlInputController(this.inputs["TI_Tag"]);
        }

        private void ControlInputController(TextInput input)
        {
            if (!input.Selected)
            {
                input.Select();
            }
            else
            {
                input.UnSelect();
            }
        }

        private void OnKeyPressed(KeyboardArgs args)
        {
            if (args.Key == Keys.Escape)
            {
                var isActive = false;

                foreach (var textInput in this.inputs.Where(textInput => textInput.Value.InputController.IsActive))
                {
                    isActive = true;
                }

                if (!isActive)
                {
                    this.OnBack(null);
                    return;
                }

                foreach (var textInput in this.inputs)
                {
                    textInput.Value.UnSelect();
                }
            }
        }
    }
}